package com.today.springandmq.Config;


import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientOptions;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.json.Json;

@Configuration
public class ESConfig {

    @Value("${spring.elasticsearch.host}")

    private String host;



    @Value("${spring.elasticsearch.port}")
    private int port;


    @Value("${spring.elasticsearch.scheme}")
    private String scheme;

    private RestClient restClient;

    @Bean
    RestClient getRestClient(){
//        超时类型	默认值	触发条件	对应异常
//        连接超时 (ConnectTimeout)	无	TCP连接建立时间超过设定值	ConnectTimeoutException
//        套接字超时 (SocketTimeout)	无	两个连续数据包间隔超过设定值	SocketTimeoutException
//        连接请求超时 (ConnectionRequestTimeout)	无	从连接池获取连接等待时间过长	ConnectionPoolTimeoutException
//
//
        return  RestClient.builder(new HttpHost(host,port,scheme))
                .setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
                    @Override
                    public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder builder) {
                        return builder.setConnectionRequestTimeout(5000)
                                .setSocketTimeout(60000)
                                ;
                    }
                })
                .setHttpClientConfigCallback(
                        new RestClientBuilder.HttpClientConfigCallback() {
                            @Override
                            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
                                return httpAsyncClientBuilder
                                        .setMaxConnTotal(50)
                                        .setMaxConnPerRoute(10)
                                        ;
                            }
                        }
                )
                .build();
    }

    @Bean(value = "myESTransport")
    public ElasticsearchTransport getESconfig(RestClient restClient){

//final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//credentialsProvider.setCredentials(
//        AuthScope.ANY,
//        new UsernamePasswordCredentials("root","123456")
//);
        JacksonJsonpMapper jsonpMapper = new JacksonJsonpMapper();

        jsonpMapper.objectMapper()
                .registerModule(new JavaTimeModule())
                .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

        //  Json的序列化 还有 去序列化
return  new RestClientTransport(restClient,jsonpMapper);






    }

@Bean(value = "ESClient")
    public ElasticsearchClient elasticsearchClient(@Autowired ElasticsearchTransport myESTransport){


        /// 装载 东西  然后api 完成
        return new ElasticsearchClient(myESTransport);

}






}
